<template>
  <div class="selector-wrap">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords" label="关键字">
          <el-input
            v-model="queryParams.keywords"
            placeholder="关键字"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item class="search-buttons">
          <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
      :data="filteredData"
      highlight-current-row
      :row-class-name="tableRowClassName"
      @current-change="handleCurrentChange"
    >
      <el-table-column label="" width="50" />
      <el-table-column label="模板名称" prop="name" />
      <el-table-column label="创建日期" prop="createTime" align="center" />
      <el-table-column label="更新时间" prop="updateTime" align="center" />
    </el-table>

    <pagination
      v-if="total > 0"
      v-model:total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="handleQuery"
    />
  </div>
</template>

<script setup lang="ts">
import ContractTempAPI, {
  ContractTempPageQuery,
  ContractTempPageVO,
} from "@/api/flow/contractTemp.api";

const queryFormRef = ref();
const queryParams = reactive<ContractTempPageQuery>({
  pageNum: 1,
  pageSize: 10,
});

const checked = ref<ContractTempPageVO>();
const handleCurrentChange = (selection: ContractTempPageVO) => {
  checked.value = selection;
};

function getChecked(): ContractTempPageVO | undefined {
  return checked.value;
}

const total = ref(0);
const filteredData = ref<ContractTempPageVO[]>();

const handleQuery = () => {
  ContractTempAPI.getPage(queryParams).then((data) => {
    filteredData.value = data.list;
    total.value = data.total;
  });
};

const handleResetQuery = () => {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
};

// 行样式
const tableRowClassName = ({ row }: { row: ContractTempPageVO }) => {
  if (row.id === checked.value?.id) {
    return "checked-row";
  }
  return "";
};

defineExpose({
  getChecked,
  handleQuery,
});
</script>

<style scoped>
.selector-wrap {
  padding: 10px;
}
</style>
